<?php
namespace app\walletyhbbadmin\controller;
use Think\Db;
use think\Collection;
use Think\page;
use think\Session;
class Muban extends AdminBase
{
    //空操作
    public function _empty()
    {
        header("HTTP/1.0 404 Not Found");
        $this->display('Public:404');
    }

    /**
     * 商品列表
     */
    public function index()
    {

        $muban = db("muban")->paginate(15, false, ['query' => input()]);
        $page = $muban->render();
        $muban = $muban->all();

        $this->assign('page', $page);
        $this->assign('muban', $muban);
        return $this->fetch('muban/index');
    }

    /**
     * 添加模板页面
     */
    public function addMuban()
    {

        return $this->fetch('muban/add');
    }

    /*
     *添加模板
     **/
    public function insertMuban()
    {
        $request = request();
        $data['name'] = input('name');
        $data['status'] = input('status');
        $data['add_time'] = time();
        if (empty($data['name'])) {
            $this->error('请补全信息', '/walletyhbbadmin/Muban/addMuban');
        }
        $file = $request->file('pic');
        if (empty($file)) {
            $this->error('请您上传模板图片', '/walletyhbbadmin/Muban/addMuban');
        } else {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'muban');
            $filename = $info->getSaveName();
            //存储图片上传的路径
            $data['pic'] = '/public/uploads/muban/' . str_replace('\\', '/', $info->getsaveName());
        }
        $bool = DB::name('muban')->insert($data);
        if ($bool) {
            $this->success('上传成功', '/walletyhbbadmin/Muban/index');
        } else {
            $this->error('上传失败', '/walletyhbbadmin/Muban/addMuban');
        }
    }


    /**
     * 修改商品上架下架状态
     */
    public function updateStatus()
    {
        $id = input('id');
        $goods_status = Db::name('muban')->where(array('id' => $id))->find();
        if ($goods_status['status'] == 1) {
            $data['status'] = 0;
        } else {
            $data['status'] = 1;
        }
        $res = Db::name('muban')->where(array('id' => $id))->update($data);
        if ($res) {
            $this->success('修改成功');
        } else {
            $this->error('参数错误');
        }
    }

    public function delMuban(){
        //获取id
        $id=input('id');
        // 获取当前分类下的商品数,大于0则不能删除--预留
        $res=Db::name('muban')->where('id',$id)->delete();
        if($res){
            $data['info']='删除成功';
            $data['status']='1';
            return json($data);
        }else{
            $data['info']='删除失败';
            $data['status']='-1';
            return json($data);
        }
    }

    /*
     *修改页面
     **/
    public function edit(){
        $where['id'] = input('id');
        $info = DB::name('muban')->where($where)->find();
        $this->assign('info',$info);
        return $this->fetch('muban/edit');
    }

    /*
     *执行修改
     **/
    public function updateMuban(){
        $request = request();
        $where['id'] = input('id');
        $data['name'] = input('name');
        $data['status'] = input('status');
        if($data['name']==null){
            $this->error('请完善信息','/walletyhbbadmin/Muban/addMuban');
        }
        $file = $request->file('pic');
        if(!empty($file)){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'muban');
            $filename = $info->getSaveName();
            //存储图片上传的路径
            $data['pic'] = '/public/uploads/muban/' . str_replace('\\', '/', $info->getsaveName());
        }
        $bool = DB::name('muban')->where($where)->update($data);
        if($bool){
            $this->success('修改成功','/walletyhbbadmin/Muban/index');
        }else{
            $this->error('修改失败','/walletyhbbadmin/Muban/addMuban');
        }
    }
}